package cn.memorynine.consumer.service;

import cn.hutool.core.bean.BeanUtil;
import cn.memorynine.dao.entity.Consumer;
import cn.memorynine.dao.entity.ForumPost;
import cn.memorynine.dao.entity.ForumPostReply;
import cn.memorynine.dao.mapper.ConsumerMapper;
import cn.memorynine.dao.mapper.ForumMapper;
import cn.memorynine.dao.mapper.ForumPostMapper;
import cn.memorynine.dao.mapper.ForumPostReplyMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.memorynine.common.model.ResponseWrapper;
import cn.memorynine.consumer.model.forum.manage.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* 管理后台-论坛管理服务
* @author 代码生成器
*/
@Slf4j
@Service
public class ForumManageService {

    @Autowired
    private ForumMapper forumMapper;

    @Autowired
    private ForumPostMapper forumPostMapper;

    @Autowired
    private ForumPostReplyMapper forumPostReplyMapper;

    @Autowired
    private ConsumerMapper consumerMapper;

    /**
     * 1.获取所有帖子列表
     * @return
     */
    public ResponseWrapper postList(PostListParam param){
        PostListResult postListResult = new PostListResult();

        Page<ForumPost> pageInfo = new Page<>(param.getPageNum(),param.getNum(),true);
        Page<ForumPost> forumPostPage = forumPostMapper.selectPage(pageInfo,
                Wrappers.<ForumPost>lambdaQuery()
                        .eq(ForumPost::getForumAid, param.getAid())
                        .like(ForumPost::getTitle, param.getSearchKey())
                        .orderByDesc(ForumPost::getTimeCreate,ForumPost::getIsDelete)
        );
        postListResult.setTotalNum((int) forumPostPage.getTotal());
        List<ForumPost> forumPosts = forumPostPage.getRecords();
        List<PostList> postList = new ArrayList<>();

        for (ForumPost forumPost : forumPosts) {
            PostList postListItem = new PostList();
            BeanUtil.copyProperties(forumPost,postListItem);
            String name = consumerMapper.selectOne(Wrappers.<Consumer>lambdaQuery().eq(Consumer::getAid, forumPost.getConsumerAid())).getName();
            postListItem.setConsumerName(name);
            postList.add(postListItem);
        }
        postListResult.setPostList(postList);
        //最后一步，返回结果
        return ResponseWrapper.markSuccess(postListResult);
    }

    /**
     * 2.删除帖子
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseWrapper delPost(DelPostParam param){
        ForumPost forumPost = new ForumPost();
        forumPost.setAid(param.getAid());
        forumPost.setIsDelete(param.getDelete());

        forumPostMapper.updateById(forumPost);

        //最后一步，返回结果
        return ResponseWrapper.markSuccess();
    }

    /**
     * 3.获取所有特定帖子回复列表
     * @return
     */
    public ResponseWrapper replyList(ReplyListParam param){
        ReplyListResult replyListResult = new ReplyListResult();

        Page<ForumPostReply> pageInfo = new Page<>(param.getPageNum(),param.getNum(),true);
        Page<ForumPostReply> forumPostReplyPage = forumPostReplyMapper.selectPage(pageInfo,
                Wrappers.<ForumPostReply>lambdaQuery()
                        .eq(ForumPostReply::getPostAid, param.getAid())
        );
        //设置总数
        replyListResult.setTotalNum((int) forumPostReplyPage.getTotal());

        List<ForumPostReply> forumPostReplies = forumPostReplyPage.getRecords();

        List<ReplyList> replyLists = new ArrayList<>();

        for (ForumPostReply forumPostReply : forumPostReplies) {
            ReplyList replyList = new ReplyList();
            BeanUtil.copyProperties(forumPostReply,replyList);
            //查询回复用户昵称
            String name = consumerMapper.selectOne(Wrappers.<Consumer>lambdaQuery().eq(Consumer::getAid, forumPostReply.getConsumerAid())).getName();
            replyList.setConsumerName(name);
            replyLists.add(replyList);
        }

        replyListResult.setReplyList(replyLists);
        //最后一步，返回结果
        return ResponseWrapper.markSuccess(replyListResult);
    }

    /**
     * 4.删除回复
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseWrapper delReply(DelReplyParam param){
        ForumPostReply forumPostReply = new ForumPostReply();
        forumPostReply.setAid(param.getAid());
        forumPostReply.setIsDelete(param.getDelete());

        forumPostReplyMapper.updateById(forumPostReply);

        //最后一步，返回结果
        return ResponseWrapper.markSuccess();
    }

}